/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.hermes.ssoLogin.saml.service;
import com.google.common.base.Preconditions;
import com.onelogin.saml2.servlet.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @project-name:hermes
 * @package-name:com.hyts.hermes.ssoLogin.saml
 * @author:LiBo/Alex
 * @create-date:2023-04-07 16:43
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
//@Component("empty")
@Slf4j
@Component
public class DefaultOktaClientLoginService implements OktaClientLoginService {


    /**
     * 登录操作
     * @param userModel
     * @param attributes
     * @return
     */
    @Override
    public String login(DefaultOktaClientSSOService.UserModel userModel, Object... attributes) {
        log.info("[hermes-ssoLogin] empty process - userName:{} - attributes:{}",userModel, Arrays.toString(attributes));
        String token = "null";
        Preconditions.checkArgument(StringUtils.isNotBlank(token),"[hermes-doLogin] can not get the token value");
        //开始重定向
        String url  = attributes[2]+"?token="+token;
        try {
            ServletUtils.sendRedirect(((HttpServletResponse)attributes[1]), url);
        } catch (IOException e) {
            log.error("[hermes-ssoLogin] occur the error or exception!",e);
        }
        return "null";
    }

    /**
     * 登出操作
     * @param userModel
     * @param attributes
     * @return
     */
    @Override
    public Boolean logout(DefaultOktaClientSSOService.UserModel userModel, Object... attributes) {
        log.info("[hermes-ssoLogout] empty process - userName:{} - attributes:{}",userModel, Arrays.toString(attributes));
        //重定向地址
        String url  = (String) attributes[2];
        try {
            ServletUtils.sendRedirect(((HttpServletResponse)attributes[1]), url);
        } catch (IOException e) {
            log.error("[hermes-ssoLogout] occur the error or exception!",e);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
